package commonwebgroup.commonwebartifact.util;

import commonwebgroup.commonwebartifact.CommandHandle.CommandCenterHandleDefault;
import commonwebgroup.commonwebartifact.Helper.ColorHelper;
import de.tr7zw.nbtapi.NBT;
import de.tr7zw.nbtapi.iface.ReadWriteNBT;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Map;
import java.util.Objects;

/**
 * @author: wuxin0011
 * @Description:
 */
public class CommandUtil {

    public static boolean checkArgs(String[] args, Player player) {
        return checkArgs(args, 2, player);
    }


    /**
     * 参数校验
     *
     * @param args 参数
     * @param l    指定参数个数 默认是2个 后续可以根据命令增加修改
     */
    public static boolean checkArgs(String[] args, Integer l, Player player) {
        if (Objects.isNull(args) || args.length < l) {
            if (!Objects.isNull(player)) {
                player.sendMessage(ColorHelper.Error("命令错误，正确指格式是: mcm <push,pull> item"));
            }
            return false;
        }
        return true;
    }

    /**
     * 校验
     *
     * @return
     */
    public static Player validPlayer(Player player) {
        Objects.requireNonNull(player, "player is not allow null");
        Objects.requireNonNull(player.getInventory(), "PlayerInventory is not allow null");
        Objects.requireNonNull(player.getInventory().getItemInMainHand(), "itemInHand is not allow null");
        return player;
    }


    public static Map<String, String> getMapData(Player player, String apiKey) {
        // 检查
        Objects.requireNonNull(apiKey, "apiKey is not allow null");
        Objects.requireNonNull(player, "player is not allow null");
        ItemStack itemInHand = player.getInventory().getItemInMainHand();

        // 检查
        Objects.requireNonNull(itemInHand, "itemInHand is not allow null");
        ReadWriteNBT nbt = NBT.itemStackToNBT(itemInHand);

        // 检查
        Objects.requireNonNull(nbt, "nbt is not allow null");
        player.sendMessage(nbt.toString());

        // 内容
        return Map.of(
                "Source", nbt.toString(),
                "UUID", player.getUniqueId().toString(),
                "Name", player.getName(),
                "SourceVersion", Bukkit.getVersion(),
                "ApiKey", apiKey
        );
    }

    public static Map<String, String> getMapData(String version, Player player, String apiKey) {
        Objects.requireNonNull(version, "version is not allow null");
        Objects.requireNonNull(player, "player is not allow null");
        Objects.requireNonNull(apiKey, "apiKey is not allow null");
        // 内容
        return Map.of(
                "UUID", player.getUniqueId().toString(),
                "Name", player.getName(),
                "SourceVersion", Bukkit.getVersion(),
                "ApiKey", apiKey
        );
    }

    /**
     * 是否是 pull item 命令
     *
     * @param args 参数
     * @return boolean
     */

    public static boolean isPullItem(String[] args) {
        return "pull".equalsIgnoreCase(args[0]) && "item".equalsIgnoreCase(args[1]);
    }

    /**
     * 是否是 push item 命令
     *
     * @param args 参数
     * @return boolean
     */
    public static boolean isPushItem(String[] args) {
        return "push".equalsIgnoreCase(args[0]) && "item".equalsIgnoreCase(args[1]);
    }

    /**
     * 设置错误消息
     *
     * @param sender sender
     */
    public static void setErrorMsg(CommandSender sender) {
        setErrorMsg(sender, "指令解析错误");
    }

    /**
     * 设置错误消息
     *
     * @param sender  sender
     * @param message 错误内容
     */
    public static void setErrorMsg(CommandSender sender, String message) {
        if (Objects.isNull(sender)) {
            sender = new CommandCenterHandleDefault();
        }
        sender.sendMessage(ColorHelper.Error(message));
    }

    /**
     * 设置ok消息
     *
     * @param sender sender
     */
    public static void setSuccessMsg(CommandSender sender) {
        setSuccessMsg(sender, "ok");
    }


    /**
     * 设置错误消息
     *
     * @param sender  sender
     * @param message 错误内容
     */
    public static void setSuccessMsg(CommandSender sender, String message) {
        if (Objects.isNull(sender)) {
            sender = new CommandCenterHandleDefault();
        }
        sender.sendMessage(ColorHelper.Success(message));
    }

    public static CommandSender validCommandSender(CommandSender sender) {
        return Objects.isNull(sender) ? new CommandCenterHandleDefault() : sender;
    }


}
